<?php
/**
 * alipay应用网关
 * @author qingyu8@staff.weibo.com
 * @date 2018/03/11
 */
namespace api\modules\v1\controllers;

use common\logic\Alipay;
use common\logic\Order;
use common\tool\Alarm;
use PHPUnit\Framework\Exception;
use yii\web\Controller;
use Yii;
use yii\web\Response;

require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/AopSdk.php';
require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/service/AlipayTradeService.php';
require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/buildermodel/AlipayTradePagePayContentBuilder.php';

class AlipayController extends Controller {
    public $enableCsrfValidation = false;

    public function actionIndex() {
        Yii::$app->response->format = Response::FORMAT_JSON;
        Alipay::query();
        exit(__METHOD__);
    }

    public function actionGateway() {
        exit(__METHOD__);
    }

    public function actionAsyncNotify() {
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = Yii::$app->params['alipay']['alipay_public_key'];
        $check = $aop->rsaCheckV1($_POST, null, $_POST['sign_type']);
        if ($check) {
            Yii::info($_POST, __METHOD__);
            if ($order = Order::getFormattedOrder($_POST['out_trade_no'])) {
                if (Yii::$app->params['payDebug'] && in_array($order['uid'], Yii::$app->params['debug_uids'])) {
                    $order['price'] = 1;
                }
                if (($_POST['total_amount'] * 100 == $order['price']) && $_POST['app_id'] == Yii::$app->params['alipay']['app_id']) {//seller_id
                    try {
                        switch ($_POST['notify_type']) {
                            case 'trade_status_sync':
                                if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                                    $res = Order::pay($_POST['out_trade_no'], $_POST['gmt_payment'],Order::PAY_TYPE_ALI);
                                    if ($res['err'] != 0) {
                                        Yii::warning($res, __METHOD__);
                                        return;
                                    }
                                    Alarm::business("订单id：" . $order['order_id'], '订单支付成功');
                                }
                                break;
                        }
                        echo "success";
                    } catch (\Exception $e) {
                        Yii::error($e, __METHOD__);
                        Alarm::sys("Exception" . $e->getMessage(), __METHOD__);
                    }
                }
            }
        } else {
            Yii::warning($_POST, __METHOD__);
        }
    }

    public function actionSyncReturn() {
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = Yii::$app->params['alipay']['alipay_public_key'];
        $check = $aop->rsaCheckV1($_GET, null, $_GET['sign_type']);
        if ($check) {
            unset($_GET['sign_type']);
            unset($_GET['sign']);
            Yii::info($_GET, __METHOD__);
            //todo 标记，跳转到前端
        } else {
            Yii::warning($_GET, __METHOD__);
        }
    }

    public function actionAuthRedirect() {
        exit(__METHOD__);
    }
}
